package cn.swenty.backstage.base.web.system.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.swenty.backstage.base.web.system.entity.SysRoleFormMap;
import cn.swenty.backstage.base.web.system.entity.SysRoleResFormMap;
import cn.swenty.backstage.base.web.system.entity.SysUserRoleFormMap;
import cn.swenty.backstage.base.web.system.mapper.SysRoleMapper;
import cn.swenty.backstage.framework.service.BaseService;
import cn.swenty.backstage.framework.util.IdGenerator;

@Service
public class RoleService extends BaseService{
	
	@Inject
	private SysRoleMapper sysRoleMapper;
	
	
	public void  save(SysRoleFormMap roleFormMap){
		try {
			roleFormMap.put("role_id", IdGenerator.getNextId());
			super.save(roleFormMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<SysRoleFormMap> findRolePage(SysRoleFormMap sysRoleFormMap){
		return sysRoleMapper.findRolePage(sysRoleFormMap);
	}
	
	
	public List<SysRoleFormMap> seletUserRole(SysRoleFormMap roleFormMap){
		return  sysRoleMapper.seletUserRole(roleFormMap);
	}
	
	public void saveRoleFunction(String roleId,String [] functions){
		
		SysRoleResFormMap sysRoleFormMap = new SysRoleResFormMap();
		sysRoleFormMap.put("role_id", roleId);
		this.delete(sysRoleFormMap);
		sysRoleFormMap.put("role_id",  roleId );
		for (String string : functions) {
			sysRoleFormMap.put("function_id", string);
			sysRoleFormMap.put("role_id", roleId);
			sysRoleFormMap.put("id", IdGenerator.getNextId());
			this.save(sysRoleFormMap);
		}
	}
	
	
	/**
	 * 通过角色用户新增
	 * 
	 * @param roleId
	 * @param users
	 */
	public void saveUserRole(String roleId,String []users){
		SysUserRoleFormMap formMap = new SysUserRoleFormMap();
		for(String u:users){
			formMap.put("user_id", "'"+u+"'");
			this.delete(formMap);
		}
		for(String u:users){
			formMap.put("id", IdGenerator.getNextId());
			formMap.put("user_id", u);
			formMap.put("role_id", roleId);
			this.save(formMap);
		}
		
		
	}
	
	/**
	 * 删除用户角色关联表
	 */
	@Transactional
	public void delUserRole(SysUserRoleFormMap userRoleBean){
		sysRoleMapper.delUserRole(userRoleBean);
	}
	
	
}
